package servlet;

import service.BanjiService;
import service.BanjiServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "BanjiDeleteServlet",urlPatterns = "/banjiDeleteServlet")
public class BanjiDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        response.setContentType ("text/html;charset=utf-8");
        request.setCharacterEncoding ("utf-8");
        // 1.获取前台页面传递过来的班级名称
        String bName = request.getParameter ("bName");
        // 2.通过Service层实现删除操作
        BanjiService banjiService = new BanjiServiceImpl ();
        int res = banjiService.BanjiDeleteService (bName);
        // 3.通过输出流向前台页面展示处理结果，然后重新请求一次数据
        PrintWriter writer = response.getWriter ();
        // 获取模块名称
        String contextPath = this.getServletContext ().getContextPath ();
        // 拼接请求路径
        String hrefPath = contextPath + "/banjiQueryServlet";
        if (res != 0) {
            writer.println ("<script> alert('班级信息管理成功'); location.href = '" + hrefPath + "';</script>");
        } else {
            writer.println ("<script> alert('班级信息管理失败'); location.href = '" + hrefPath + "';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost (request,response);
    }
}
